package th.ac.mahidol.ict.is.mpay;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener
		 {
	/** Called when the activity is first created. */
	private static String TAG = "Main";
	private Button submit;
	private EditText telNo;
	private String price;
	private Button configBtn;
	private String host;
	private String path = "payto/";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		submit = (Button) findViewById(R.id.submit);
		configBtn = (Button) findViewById(R.id.config);
		price = "";
		telNo = (EditText) findViewById(R.id.telNumber);
		submit.setOnClickListener(this);
		configBtn.setOnClickListener(this);
		
	}
	

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	

	/**
     * 
     */
	public void onClick(View v) {
		//Toast.makeText(this, getMy10DigitPhoneNumber(), Toast.LENGTH_SHORT).show();
		if (v == submit) {
			payto();
		} else if (v == configBtn) {
			showConfigAlert();
		}
	}

	private String getMyPhoneNumber(){
		TelephonyManager mTelephonyMgr;
		mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
		return mTelephonyMgr.getLine1Number();
		}

	private String getMy10DigitPhoneNumber(){
		try {
			Log.d(TAG, "phone no:"+getMyPhoneNumber());
		} catch (Exception e) {
			Log.d(TAG, "can not get phone number");
		}
		return "0824542697";
	}
	private String requestString() {
		String requestStr = "";
		requestStr = getMy10DigitPhoneNumber();
		requestStr += "/";
		requestStr += price.toString();
		requestStr += "/";
		requestStr += telNo.getText().toString();
		return requestStr;
	}
	/**
	 * 
	 */
	private void showConfigAlert() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Config");
		alert.setMessage("Enter host's base url.");

		// Set an EditText view to get user input
		final EditText input = new EditText(this);
		input.setHint("localhost/");
		if(host != null) {
			input.setText(host);
		}
		input.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			

			public void onClick(DialogInterface dialog, int whichButton) {
				String value = input.getText().toString();
				if(value.startsWith("http://")) {
					host = value;
				} else {
					host = "http://"+value;
				}
				
				
				Toast.makeText(Main.this, value, Toast.LENGTH_SHORT).show();
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						;
					}
				});
		alert.show();
	}

	private void displayDialog(String display) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(display).setCancelable(false).setPositiveButton(
				"OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						Log.d(TAG, "Sent confirm");
						try {
							FileGetContent.getContents(host+path+requestString(), "UTF-8");
						} catch (Exception e) {
							Log.d(TAG, "Can not send request");
						}
					}
				}).setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	

	private void payto() {

		Log.d(TAG, "submit clicked " + price.toString() + " "
				+ telNo.getText().toString());
		if (telNo.getText().length() != 0 && price.length() != 0) {
			displayDialog("Pay to " + telNo.getText().toString() + "\n"
					+ "with " + price.toString() + "  ฿");
		}
	}

}